module SendFile

	def xsend_file(path = nil, options = {})
		require 'fileutils'
		path ||= "#{RAILS_ROOT}/public/images/qmark.jpg"
		options.reverse_merge!({ :type => "application/octet-stream", :disposition => "inline; filename=#{File.basename(path)}", :content_size => 0 })
		_method = SENDFILE_METHOD || "normal_sendfile"
		self.send _method, path, options
	end

	def normal_sendfile(path, options = {})
		send_file(path, :type => options[:type], :disposition => options[:disposition], :filename => (options[:filename].blank?) ? nil : options[:filename])
	end

	def apache2_sendfile(path, options = {})
		response.headers["X-Sendfile"] = path
		response.headers['Content-Type'] = options[:type]
		response.headers['Content-Disposition'] = "#{options[:disposition]}; filename=#{options[:filename]}"
		response.headers['Content-length'] = options[:content_size]
		response.headers['Cache-Control'] = 'must-revalidate'
		render :nothing => true
	end

	def nginx_sendfile(path, options = {})
		response.headers["X-Accel-Redirect"] = path
		response.headers['Content-Type'] = options[:type]
		response.headers['Content-Disposition'] = "#{options[:disposition]}; filename=#{options[:filename]}"
		response.headers['Content-length'] = options[:content_size]
		response.headers['Cache-Control'] = 'must-revalidate'
		render :nothing => true
	end

end
